<?php
class DataDictionaryController extends CAdmin {
	function __construct(){
		parent::__construct();
	}
	
	public function init(){
		include self::admin_tpl('datadictionary');
	}
	
	public function data_list(){		
		$optiondb = new DataDictionary_Model();
		if(isset($_POST['dosubmit'])){
			$data_type = $_POST['data_type'];
			$options = $_POST['option'];
			foreach ($options as $op) {
				if((!isset($op['data_id']) || empty($op['data_id'])) && (!isset($op['del']) || empty($op['del']))){
					$optiondb->insert(array('data_name'=>$op['data_name'],'type'=>$data_type,'code'=>$op['code']));
				} 
				if ((!isset($op['del']) || empty($op['del'])) && !empty($op['data_id'])){
					$optiondb->update(array('data_name'=>$op['data_name'],'type'=>$data_type,'code'=>$op['code']),array('data_id'=>$op['data_id']));
				} 
				if (isset($op['del']) && isset($op['data_id']) && !empty($op['data_id']) && !empty($op['del'])) {
					$optiondb->delete(array('data_id'=>$op['data_id']));
				}
			}
			$this->__cache();
			self::showmessage('操作成功！','?m=admin&c=datadictionary');			
		}else{
			$data_type = $_GET['data_type'];
			$type_name = $_GET['type_name'];
			$options = $optiondb->select(array('type'=>$data_type),'data_id,data_name,code');			
			include self::admin_tpl('data_list');
		} 
	}
	
	/**
	 * 缓存
	 */
	private function __cache(){
		$optiondb = new DataDictionary_Model();
		$options = $optiondb->select();				
		$array =array();
		foreach ($options as $op) {
			$array[$op['type']][$op['code']] = $op['data_name'];
		}
		$cache = new Caches();		
		$cache->stecache('option', $array, 'commons');
		return $options;
	}
}